package com.union.official.common.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 实体类工具类
 *
 * @author 杨 强
 * @package com.union.official.common.util
 * @since 2024-03-07 11:40
 */
@Component
public class EntityUtil {

    /**
     * 数据库表PO类转换为数据传输的VO类
     * @param t PO类
     * @param voClass VO类class,用于反射实例化VO
     * @param <T>
     * @param <R>
     * @return VO类
     */
    public static <T, R> R toToVo(T t, Class<R> voClass){
        try {
            R vo = voClass.getDeclaredConstructor().newInstance();
            BeanUtils.copyProperties(t, vo);
            return vo;
        } catch (Exception e) {
            throw new RuntimeException("服务器内部异常");
        }
    }

    /**
     * 数据库表PO类转换为数据传输的VO类
     * @param t PO类
     * @param voClass VO类class,用于反射实例化VO
     * @param <T>
     * @param <R>
     * @return VO类
     */
    public static <T, R> List<R> toToVo(List<T> t, Class<R> voClass){
        try {
            List<R> result = new ArrayList<>();
            for (T t1 : t) {
                R vo = voClass.getDeclaredConstructor().newInstance();
                BeanUtils.copyProperties(t1, vo);
                result.add(vo);
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException("服务器内部异常");
        }
    }
}
